package com.lonty.intelligence.controller;

import com.lonty.intelligence.api.Result;
import com.lonty.intelligence.service.ImportDataService;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author 陈璇
 * @Date 2020/5/19 14:10
 * @Version 1.0
 */
@RestController
@RequestMapping("app/importData")
@Validated
@Api(tags = "导入数据接口", produces = "application/json")
public class ImportDataController {

    @Resource
    private ImportDataService importDataService;

    @ApiOperation(value = "导入数据", httpMethod = "POST")
    @PostMapping(value = "importData", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public Result importData(@ApiParam(value = "上传的文件", required = true) MultipartFile multipartFile) throws Exception {
        String fileName =  multipartFile.getOriginalFilename();
        InputStream inputStream = multipartFile.getInputStream();
        return Result.builder()
                .data(importDataService.importData(inputStream, fileName))
                .build();
    }

}
